//
//  FunSelctTabView.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/21.
//  Copyright © 2016年 game. All rights reserved.

#import "FunSelctTabView.h"
#import "SelectionCell.h"
#import "FunselctSectionZeroCell.h"
#import "FunSelectModel.h"
#import "FunPagerModel.h"
#import "LoginController.h"
#import "SeleCtContrl.h"
#import "OtherInformationCtrl.h"
#import "FunSelectTabHead.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface FunSelctTabView ()<UITableViewDataSource,UITableViewDelegate,SelectionCellDeleget>
@property (nonatomic,strong) FunSelectModel *model;
@property (nonatomic,assign) CGFloat selectCellhight;
@property (nonatomic,strong) NSMutableArray *topAry;
@property (nonatomic,strong) NSMutableArray *listAry;
@property (nonatomic,strong) FunPagerModel *pageModel;
@property (nonatomic,assign) NSInteger pageCont;
@property (nonatomic,strong) FunSelectTabHead *tableHead;

@property (nonatomic,strong) NSMutableDictionary *cellhightDic;
@end

@implementation FunSelctTabView

-(NSMutableDictionary *)cellhightDic{

    if (!_cellhightDic) {
        _cellhightDic  = [NSMutableDictionary dictionary];
    }
    return _cellhightDic;
}
-(NSMutableArray *)topAry{
    if (!_topAry) {
        _topAry = [NSMutableArray array];
    }
    return _topAry;
}

-(NSMutableArray *)listAry{
    if (!_listAry) {
        _listAry = [NSMutableArray array];
    }
    return _listAry;
}

-(void)AchiveDataWhithFid:(NSInteger)fid AndPage:(NSInteger)page{

    NSString *Url = [NSString stringWithFormat:FunSelectAllDataUrl,(long)fid,(long)page];

    if ([self.isEssence isEqualToString:@"YES"]) {
        Url = [NSString stringWithFormat:FunSelectEssenceDataUrl,(long)fid,(long)page];
    }


    [[Manager sharedManager]RequestWhithURL:Url AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        self.model = [[FunSelectModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        self.pageModel = self.model.pager;
        self.tableHead.model = self.model.forum;
        [self.topAry addObjectsFromArray:self.model.top_threadlist];
        [self.listAry addObjectsFromArray:self.model.forum_threadlist];
        
        if (!self.tableHead.model) {
            self.tableHeaderView = nil;
        }else{
            self.tableHeaderView = self.tableHead;
        }
        
         self.isShowImage = YES;
                
        [UIView transitionWithView:self duration:0.1 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            [self reloadData];
        } completion:nil];
        
        [self.header endRefreshing];
        [self.footer endRefreshing];
    }];
}

-(void)refreshHeader{
    [self.topAry removeAllObjects];
    [self.listAry removeAllObjects];
    [self AchiveDataWhithFid:self.fid AndPage:1];
    
    self.pageCont = 1;
}

-(void)refreshFooter{

    self.pageCont += 1;
    if (self.pageCont>self.pageModel.total_page.integerValue) {
        
        [self.footer noticeNoMoreData];
    }else{
        [self AchiveDataWhithFid:self.fid AndPage:self.pageCont];
    }

}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = tabGropBackColor;
        [self registerNib:[UINib nibWithNibName:@"FunselctSectionZeroCell" bundle:nil] forCellReuseIdentifier:@"ZeroCell"];
        
        [self registerNib:[UINib nibWithNibName:@"SelectionCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        self.header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshHeader)];
        self.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFooter)];
        
        self.pageCont = 1;

        self.separatorStyle = 0;
        
        self.tableHead = [[NSBundle mainBundle]loadNibNamed:@"FunSelectTabHead" owner:nil options:nil].lastObject;
        
        self.tableHead.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
        self.tableHeaderView = self.tableHead;
        self.tableHead.alpha = 0;
        
    }
    return self;
}
-(void)setFid:(NSInteger)fid{
    _fid = fid;
    [self AchiveDataWhithFid:_fid AndPage:1];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section==0) {
        
        return self.topAry.count;
    }else{
    
        return self.listAry.count;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 47;
    }else{
    
        return 1+[[self.cellhightDic valueForKey:[NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row]] floatValue];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==0) {
        
        FunselctSectionZeroCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZeroCell"];
        if (self.topAry.count>0) {
        
            cell.model = self.topAry[indexPath.row];
        }
    
        return cell;
        
    }else{
        SelectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (self.listAry.count>0) {
            cell.model = self.listAry[indexPath.row];
            
//            NSLog(@"model------%@",cell.model);
            cell.deleget = self;
            
            [self.cellhightDic setValue:[NSString stringWithFormat:@"%f",cell.height] forKey:[NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row]];
        }
   
        return cell;
    }
}
#pragma mark--SelectionCell中的代理方法
-(void)CommentSelect:(NSString *)tid Row:(NSInteger)row{
    NSString *url = [NSString stringWithFormat:MainSelet,tid];
    
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = tid;
    str.url = url;
    str.isSlide = YES;
    str.rowOfCell = row;
    [self.fatherCtrl.navigationController pushViewController:str animated:YES];

}

-(void)UserSpaceClick:(NSString *)uid{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.fatherCtrl.navigationController pushViewController:space animated:YES];

}

-(void)PramisNoLogin{
    LoginController *login = [LoginController new];
    [self.fatherCtrl.navigationController pushViewController:login animated:YES];
}

-(void)PraimSelect:(NSInteger)section{
    thread_listModel *model = self.model.forum_threadlist[section];
    model.isZan = YES;
}

-(void)PraimFile:(NSString *)message{

    [Manager GetHudWhithText:message AndFatherView:self];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *tid;
    
    if (indexPath.section==0) {
        FunselctSectionZeroCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        tid = cell.tid;
    }
    
    if (indexPath.section==1) {
        SelectionCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        tid = cell.tid;
    }
    
     NSString *url = [NSString stringWithFormat:MainSelet,tid];
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = tid;
    str.url = url;
    str.isSlide = NO;
    str.rowOfCell = indexPath.row;
    [self.fatherCtrl.navigationController pushViewController:str animated:YES];
}
@end
